<?php
/**
 * 角色控制器
 * User: Lin Hui
 * Date: 2016/12/14
 * Time: 2:13
 */

namespace Admin\Controller;


class RoleController extends BaseController
{
    public function saveDataTrans(&$data)
    {
        if($data['role_id'] == 0){
            unset($data['role_id']);
        }
        $data['update_time'] = time();
        $data['create_time'] = time();
        return $data;
    }

    /**
     * 角色配置
     */
    public function setting()
    {
        $model = D('role');
        $role_auth_model = D('role_auth');
        $role_id = I('role_id', 0, 'intval');
        $role_auth_map = array('role_id'=>$role_id);
        if(IS_POST){
            $data = I('post.');
            if($model->create() && $model->save() !== false){
                $role_auth_model->where($role_auth_map)->delete();
                $add = array();
                foreach($data['auth_id'] as $auth_id){
                    if($auth_id){
                        $add[] = array(
                            'auth_id'=>$auth_id,
                            'role_id'=>$role_id,
                        );
                    }
                }
                if($role_auth_model->addAll($add) || count($add) == 0){
                    $this->responseAjax('保存成功', true, array(), U('index'));
                } else {
                    $this->responseAjax('保存权限失败', false);
                }
            }

            $this->responseAjax('保存失败', false);
        }
        $info = $model->find($role_id);

        $menu_list = D('menu')->getMenuListOrderByParentId(0);

        foreach($menu_list as &$menu){
            $menu['auth'] = D('auth')->where(array('menu_id'=>$menu['menu_id']))->select();
        }
        $auth_list = $role_auth_model->where($role_auth_map)->getField('auth_id', true);
        $this->assign('info', $info);
        $this->assign('menu_list', $menu_list);
        $this->assign('auth_list', $auth_list);
        $this->display();
    }


}